package com.newsoft.frame.codegen;

import java.io.File;
import java.util.List;

import org.springframework.stereotype.Component;

import com.cw.jeeyt.common.enums.EnumGenerateCodeType;
import com.cw.jeeyt.common.enums.EnumGenerateProjectCode;
import com.cw.jeeyt.common.enums.EnumTemplateType;
import com.cw.jeeyt.service.bo.system.SystemTemplateModuleInfoBo;
import com.newsoft.frame.codegen.parameter.ParamBundle;
import com.newsoft.frame.codegen.parameter.ParamContext;

/**
 * To generate the page file (jsp, js, etc.)
 *
 * @author chenw
 */
@Component
public class PageFileGenerator extends AbstractSourceFileGenerator {
    @SuppressWarnings("unchecked")
    @Override
    public void generateConcreteFiles(ParamContext paramContext) throws Exception {

        String outputParentDir = paramContext.getPageOutputPath();

        List<SystemTemplateModuleInfoBo> stmibList = (List<SystemTemplateModuleInfoBo>) paramContext.getAttributes()
            .get("systemTemplateModuleInfoList");
        String generateProjectCode = (String)paramContext.getAttributes().get("generateProjectCode");
        for (ParamBundle paramBundle : paramContext.getParamBundles()) {
            if (paramBundle.getFieldList() == null || paramBundle.getFieldList()
                .isEmpty()) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Ignore this param bundle since no field list found.");
                }
                continue;
            }

            if (logger.isDebugEnabled()) {
                logger.debug("To create page file source for param bundle: " + paramBundle);
            }
            //生成代码
            if (stmibList != null && !stmibList.isEmpty()) {
                for (SystemTemplateModuleInfoBo stmib : stmibList) {
                    if (EnumTemplateType.PAGE.getCode()
                        .equals(stmib.getTemplateModuleType())) {
                        createSingleFile(outputParentDir, paramBundle, stmib.getTemplateModuleName(),generateProjectCode);
                    }
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Finished to create page file source at directory: " + outputParentDir);
            }
        }
    }

    private void createSingleFile(String outputParentDir, ParamBundle paramBundle, String templateName,String generateProjectCode)
    throws Exception {
        String fileNamePrefix = paramBundle.getModuleName();
        String generateType = paramBundle.getGenerateType();

        Configuration configuration;

        if (EnumGenerateCodeType.PAGE.getCode()
            .equals(generateType)) {
            configuration = Configuration.getPageConfig();
        } else if (EnumGenerateCodeType.CLASS.getCode()
            .equals(generateType)) {
            configuration = Configuration.getClassConfig();
        } else if (EnumGenerateCodeType.JDBC.getCode()
            .equals(generateType)) {
            configuration = Configuration.getJdbcConfig();
        } else if (EnumGenerateCodeType.PROPERTIES.getCode()
            .equals(generateType)) {
            configuration = Configuration.getDefault();
        } else {
            throw new Exception("不能识别当前生成代码方式");
        }
        String template = configuration.getPageTemplatePath() + "/" + templateName;
        File file = new File(template);
        if (!file.exists()) {
            return;
        }
        String targetFilePath = outputParentDir + "/" + Constant.PAGE_OUTPUT_PATH + "/" + templateName.replaceAll("XXX", fileNamePrefix);
        if(EnumGenerateProjectCode.RUOYI.getCode().equals(generateProjectCode)){
            if(templateName.contains(".vue")){
                targetFilePath = outputParentDir + "/" + Constant.PAGE_OUTPUT_PATH + "/" + fileNamePrefix + "/"
                    + templateName.replaceAll("XXX", "index");
            }
        }
        templateEngine.process(paramBundle, templateName,targetFilePath);
    }
}
